/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.lastfm;

import aiaudio.lastfm.hbase.SchemeCommunicator;
import de.umass.lastfm.CallException;
import de.umass.lastfm.PaginatedResult;
import de.umass.lastfm.User;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author nastya
 */
public class SimpleFileUsersExtracter extends IUsersExtracter {

    private static List<String> initialUsers = new Vector<String>();

    static {
        initialUsers.add("anastasiyasweet");
        initialUsers.add("joicy_banichek");
        initialUsers.add("cheshirez");
    }
    private List<User> loadedUsers = new Vector<User>();
    private int userCount;

    public SimpleFileUsersExtracter(int userCount) {
        this.userCount = userCount;
    }

    public void load() {
        List<User> newUsers = initUsers();

        while (newUsers.size() > 0 && loadedUsers.size() < userCount) {
            newUsers = loadNewUsers(newUsers);
            loadedUsers.addAll(newUsers);
        }
    }

    public void saveToTable(SchemeCommunicator com) throws IOException{
        saveToTable(com, loadedUsers);
    }
    
    public void saveToTable(SchemeCommunicator com, int max) throws IOException{
        saveToTable(com, loadedUsers.subList(0, max));
    }
    

    private List<User> loadNewUsers(List<User> lastUsers) {
        List<User> newUsers = new Vector<User>();
        for (User user : lastUsers) {
            try {
                PaginatedResult<User> friends = User.getFriends(user.getName(), false, 1, 100, LastFmDownloaderHBase.API_KEY);
                for (User friend : friends.getPageResults()) {
                    tryAddNewUser(friend.getName(), newUsers);
                }
            } catch (CallException ex) {
                System.out.println("        Call Exception at user friends: " + user.getName());
            }


            try {
                PaginatedResult<User> friends = User.getFriends(user.getName(), false, 1, 100, LastFmDownloaderHBase.API_KEY);
                Collection<User> neighbours = User.getNeighbours(user.getName(), 100, LastFmDownloaderHBase.API_KEY);
                for (User friend : neighbours) {
                    tryAddNewUser(friend.getName(), newUsers);
                }
            } catch (CallException ex) {
                System.out.println("        Call Exception at user neighbours: " + user.getName());
            }
        }
        return newUsers;
    }

    private void tryAddNewUser(String friendName, List<User> newUsers) {
        User info = User.getInfo(friendName, LastFmDownloaderHBase.API_KEY);
        if (!loadedUsers.contains(info)
                && !newUsers.contains(info)) {
            newUsers.add(info);
        }
    }

    private List<User> initUsers() {
        List<User> newUsers = new Vector<User>();
        for (String userName : initialUsers) {
            tryAddNewUser(userName, newUsers);
        }

        return newUsers;
    }
}
